<template>
  <div class="page-warapper">
    <el-card class="card">
      <!-- 公司信息行 -->
      <el-row>
        <!-- 左侧 -->
        <TreeTools :data="comoanyInfo" :is-company="true" @showDialog="showDialog" />
      </el-row>
      <!-- 右侧 -->

      <hr>
      <!-- 部门树型结构 -->
      <el-tree :data="treeData" default-expand-all>
        <template v-slot="scope">
          <!-- 部门名称:{{ scope.data.name }} 负责人：{{ scope.data.manager }} -->

          <TreeTools :data="scope.data" @showDialog="showDialog" @showEditDialog="showEditDialog" @colet="loadLiast" />
        </template>
      </el-tree>
    </el-card>
    <!-- <el-button @click="isShowDialog=true">打开弹窗</el-button> -->
    <DeptDialog ref="deptDialog" :is-show-dialog="isShowDialog" :data="data" @cancel="isShowDialog=false" @updata="loadLiast" />
  </div>
</template>

<script>
import DeptDialog from './components/dept-dialog.vue'
import { getDepts, getDeptDetail } from '@/api/departments'
import TreeTools from './components/tree-tools.vue'
import { listToTree } from '@/utils/index'
export default {
  components: {
    TreeTools,
    DeptDialog
  },
  data() {
    return {
      comoanyInfo: {
        name: '黑马程序员',
        manager: '负责人',
        id: ''
      },
      treeData: [],
      isShowDialog: false,
      data: {}
    }
  },
  created() {
    this.loadLiast()
  },
  methods: {
    async loadLiast() {
      const { depts } = await getDepts()
      // console.log('部门东西', depts)
      this.treeData = listToTree(depts, '')
    },
    showDialog(data) {
      this.data = data
      // console.log('父组件点击接收组件', data)
      this.isShowDialog = true
    },
    async showEditDialog(data) {
      // 拿取数据
      const res = await getDeptDetail(data.id)
      // console.log('获取数据', res)
      this.$refs.deptDialog.formData = res
      this.isShowDialog = true
    }

  }

}
</script>

<style lang="scss" scoped>
.card {
  padding: 30px 140px;
}
.dropdown-btn {
  cursor: pointer;
}
.info{
  width: 100%;
}
</style>
